package com.ctrip.cap.client.impl;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.cap.client.AvatarServiceClient;
import com.ctrip.cap.client.impl.base.BaseClientImpl;
import com.ctrip.cap.client.property.CapClientProperties;
import com.ctrip.cap.domain.LogCase;
import com.ctrip.cap.domain.LogStep;
import com.ctrip.cap.domain.api.APIRun;
import com.ctrip.cap.domain.dto.CaseDTO;
import com.ctrip.cap.exception.CapDataException;

public class CapAvatarDataServiceClientImp extends BaseClientImpl implements
		AvatarServiceClient {

	private static final Logger logger = LoggerFactory
			.getLogger(CapAvatarDataServiceClientImp.class);

	public CapAvatarDataServiceClientImp() {
		super();
	}

	@Override
	public void helloWorld() {

		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.capDataUrl)
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS).path("service")
				.path("var");
		Invocation.Builder invocationBuilder = webTarget.queryParam("param",
				"小明").request();
		Response response = invocationBuilder.get();
		// http://127.0.0.1:8080/cap-data/service/var?greeting=Hello World!

		if (response.getStatus() != 200) {
			throw new CapDataException(response.readEntity(String.class));
		} else {
			System.out.println(response.readEntity(String.class));
		}
	}

	@Override
	public Integer createCaseID(CaseDTO caseDTO) {

		return this.baseCall("/service/case", caseDTO, HttpMethodType.POST,
				Integer.class);

	}

	@Override
	public void writeBackCaseResult(int caseID) {

		this.baseCall("/service/case", caseID, HttpMethodType.PUT);

	}
	
	@Override
	public void writeBackCaseResult(LogCase logCase) {

		this.baseCall("/service/case-result", logCase, HttpMethodType.PUT);

	}

	@Override
	public void writeBackRunResult(int jobID) {

		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.capDataUrl)
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS).path("service")
				.path("job");
		Invocation.Builder invocationBuilder = webTarget.queryParam("jobID",
				jobID).request();
		Response response = invocationBuilder.put(Entity.entity(jobID,
				MediaType.APPLICATION_JSON));

		if (response.getStatus() != 200) {
			throw new CapDataException(response.readEntity(String.class));
		}

	}

	@Override
	public void addLogStep(LogStep logStep) {

		this.baseCall("/service/logstep", logStep, HttpMethodType.POST);

	}

	@Override
	public void addRemark(LogCase logCase) {

		this.baseCall("/service/case/remark", logCase, HttpMethodType.PUT);
	}

	@Override
	public Long addAPIRun(APIRun apiRun) {
		return this
				.baseCall("/apirun", apiRun, HttpMethodType.POST, Long.class);
	}

	@Override
	public LogCase createCase(CaseDTO caseDTO) {
		return this.baseCall("/service/case2", caseDTO, HttpMethodType.POST,
				LogCase.class);

	}

}
